﻿using AutoMapper;
using IdGen;
using KWT.DRG_DIP.Common;
using KWT.DRG_DIP.DO.QC;
using KWT.DRG_DIP.DTO.Settlement.List;
using KWT.DRG_DIP.ISvc;
using KWT.DRG_DIP.PO.QC;
using KWT.DRG_DIP.PO.Settlement;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace KWT.DRG_DIP.Svc
{
    public class QCSvc : IQCSvc
    {
        public IMapper _mapper { get; set; }
        public IIdGenerator<long> snow_id { get; set; }
        private readonly IHttpClientFactory _qc_http;
        public QCSvc(IHttpClientFactory qc_http)
        {
            _qc_http = qc_http;
        }

        public async Task<(List<QC_Err_Item> errList, bool flag)> GetErrItemAsync(InAddOrUpdate dto, string listID)
        {
            var http = _qc_http.CreateClient("QC");
            string qc_url = _Config.GetValue<string>("qc_url");
            var wt4 = _mapper.Map<WT4>(dto);
            var rqs = JsonConvert.SerializeObject(wt4);
            var rsp = await http.PostAsync(qc_url, new StringContent(rqs, Encoding.UTF8, "application/json"));
            if (rsp.IsSuccessStatusCode)
            {
                var result = await rsp.Content.ReadAsStringAsync();

                var dic = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(result);
                var _r = dic.TryGetValue("__RESULT__", out string[] val);
                if (_r)
                {
                    if (!val[0].Equals("SUCCESS"))
                    {
                        return (new List<QC_Err_Item>(), false);
                    }
                    else
                    {
                        var qcItems = new List<QC_Err_Item>();
                        foreach (var property in dic.Keys)
                        {
                            if (!property.StartsWith('_'))
                            {
                                var qcItem = new QC_Err_Item() { Property = property };
                                if (dic.TryGetValue(property, out string[] errCodes))
                                {
                                    foreach (var code in errCodes)
                                    {
                                        qcItem.QIID = "QI" + snow_id.CreateId().ToString();
                                        qcItem.ListID = listID;
                                        qcItem.ErrCode = code;
                                        qcItems.Add(qcItem);
                                    }
                                }
                            }
                        }
                        return (qcItems, true);
                    }
                }
                else
                {
                    return (new List<QC_Err_Item>(), false);
                }
            }
            else
            {
                return (new List<QC_Err_Item>(), false);
            }
        }
    }
}
